package com.jed.xxh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.jed.xxh.MainActivity;
import com.jed.xxh.R;
import com.jed.xxh.entity.MyUser;
import com.jed.xxh.utils.L;
import com.jed.xxh.utils.ShareUtil;
import com.jed.xxh.utils.T;
import com.jed.xxh.view.CustomDialog;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 项目名：SmartButler<br/>
 * 包名：  com.jed.xxh.ui<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/25 15:51<br/>
 * 描述：用户登录<br/>
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_registered;
    private Button btn_login;
    private EditText log_et_name;
    private EditText log_et_password;
    private CheckBox keep_password;
    private TextView log_tv_forget;
    private CustomDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        btn_registered = findViewById(R.id.btn_registered);
        btn_login = findViewById(R.id.btn_login);
        log_et_name = findViewById(R.id.log_et_name);
        log_et_password = findViewById(R.id.log_et_password);
        keep_password = findViewById(R.id.keep_password);
        log_tv_forget = findViewById(R.id.log_tv_forget);
        btn_registered.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        log_tv_forget.setOnClickListener(this);

        Boolean isCheck = ShareUtil.getBoolean("keepPass", false);
        keep_password.setChecked(isCheck);
        if (isCheck) {
            String userName = ShareUtil.getString("userName", "");
            log_et_name.setText(userName);
            log_et_name.setFocusable(true);
            log_et_name.setSelection(userName.length());
            log_et_password.setText(ShareUtil.getString("userPassword", ""));
        }

        dialog = new CustomDialog(this, 100, 100, R.layout.dialog_loding, R.style.Theme_dialog, Gravity.CENTER, R.style.pop_anim_style);
        dialog.setCancelable(false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.log_tv_forget://忘记密码
                startActivity(new Intent(this, ForGetPasswordActivity.class));
                break;
            case R.id.btn_registered://注册
                startActivity(new Intent(LoginActivity.this, RegisteredActivity.class));
                break;
            case R.id.btn_login://登录
                doLogin();
                break;

        }
    }

    /**
     * 用户登录逻辑
     */
    private void doLogin() {
        String name = log_et_name.getText().toString().trim();
        String password = log_et_password.getText().toString();
        boolean isEmpty = !TextUtils.isEmpty(name) && !TextUtils.isEmpty(password);
        if (isEmpty) {
            dialog.show();
            MyUser myUser = new MyUser();
            myUser.setUsername(name);
            myUser.setPassword(password);
            myUser.login(new SaveListener<MyUser>() {
                @Override
                public void done(MyUser myUser, BmobException e) {
                    dialog.dismiss();
                    if (e == null) {
                        if (myUser.getEmailVerified()) {
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        } else {
                            L.i("请前往邮箱验证");
                            T.showShortToast("请前往邮箱验证");
                        }
                    } else {
                        L.i("登录失败======错误码为：" + String.valueOf(e.getErrorCode()));
                        T.showShortToast("登录失败");
                    }
                }
            });
        } else {
            L.i(this.getString(R.string.text_tost_empty));
            T.showShortToast(this.getString(R.string.text_tost_empty));
        }
    }

    /**
     * 如果用户输入完用户名和密码后直接退出了程序，
     * 则需要根据记住密码按钮的选择状态保存当前用户输入的信息
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //保存记住密码按钮的状态
        ShareUtil.putBoolean("keepPass", keep_password.isChecked());
        if (keep_password.isChecked()) {
            ShareUtil.putString("userName", log_et_name.getText().toString().trim());
            ShareUtil.putString("userPassword", log_et_password.getText().toString().trim());
        } else {
            ShareUtil.deleShare("userName");
            ShareUtil.deleShare("userPassword");
        }
    }
}
